package com.library.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.library.dao.BookTitleDAO;
import com.library.dao.DAOFactory;
import com.library.dao.MySQLDAOFactory;

/**
 * Servlet implementation class CheckBookStatus
 */
@WebServlet("/CheckBookStatus")
public class CheckBookStatus extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckBookStatus() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bookTitleId = request.getParameter("titleid");
		int id = Integer.parseInt(bookTitleId);
		DAOFactory factory = new MySQLDAOFactory();
		BookTitleDAO bookDAO = factory.getBookTitleDAO();
		// dem so sach con dc muon trong table: booktitle
		int availableBook = bookDAO.getNoBookWithState(Resource.AVAILABLE, id);
		//dem so hang trong reservation ma trang thai la chuan bi giao
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		if (availableBook > 0){
			response.getWriter().write(Resource.BOOK_BORROWABLE);
		} else {
			response.getWriter().write(Resource.ALL_BOOK_BORROWED);
		}
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
